<?php
require_once (MLEngine . 'MakiFactory/MySQLMakis/MySQLFactory.php');
require_once (MLEngine . 'MakiFactory/Makis/UserMaki.php');
require_once (MLSources . 'DBConexion.php');

class MySQLUserMaki implements UserMaki {
	
	public function actualizar($user) {
		//se abre la conexion :D
		$conexion = DBConexion::getConexion ();
		//se pasa los parametros en la consulta :)
		$query = sprintf ( "update user set coduser = %d, coduser = %d where coduser = %d", $user->getcoduser (), $user->getCoduser (), $user->getcoduser () );
		//se ejecuta la consulta
		return $conexion->query ( $query );
		//cerrando la conexion
		$conexion->close ();
	}
	
	public function eliminar($user) {
		//se abre la conexion :D	
		$conexion = DBConexion::getConexion ();
		//se pasa los parametros en la consulta :)
		$query = sprintf ( "delete from user where coduser=%d", $user->getCoduser () );
		
		return $conexion->query ( $query );
		//cerrando la conexion
		$conexion->close ();
	}
	
	public function insertar($user) {
		//se abre la conexion :D
		$conexion = DBConexion::getConexion ();
		//se pasa los parametros en la consulta :)
		

		$query = sprintf ( "insert into user values (null,'%s','%s','%s','%s','%s','%s')", $user->getUsername (), $user->getPass (), $user->getNomuser (), $user->getApeuser (), $user->getFreg (), $user->getFact () );
		//se ejecuta la consulta
		return $conexion->query ( $query );
		//cerrando la conexion
		

		$conexion->close ();
	}
	
	public function buscar($user) {
		$conexion = DBConexion::getConexion ();
		$query = sprintf ( "select * from user " );
		$rs = $conexion->query ( $query );
		
		while ( ($row = $rs->fetch_assoc ()) != false ) {
			print ("[" . $row ["coduser"] . " & " . $row ["freg"] . " & " . $row ["pass"] . "]</br>") ;
		} /* else {
			print ("vacio") ;
		}*/
		
		echo "num devueltas: " . $rs->num_rows;
		$rs->free ();
		
		//cerrando la conexion
		$conexion->close ();
	
	}
	
	public function validar($user) {
		$conexion = DBConexion::getConexion ();

		
		$query = sprintf ( "select * from user where username='%s' and pass='%s'", $user->getUsername (), $user->getPass () );

		$rs = $conexion->query ( $query );
		

		if (($row = $rs->fetch_assoc ()) != false) {
			
			$user->setCoduser ( $row ["coduser"] );
			$user->setNomuser ( $row ["nomuser"] );
			$user->setApeuser ( $row ["apeuser"] );
			$user->setFreg ( $row ["freg"] );
			$user->setFact ( $row ["fact"] );
		}
		
		return $user;
		
		//cerrando la conexion
		$conexion->close ();
	}

}

?>